require(ltm)
require(plink)
require(haven)
require(readr)
require(dplyr)
require(psych)

voter2003 <- read_sav(
  "utas060104.sav", 
  encoding = "CP932", 
  user_na = FALSE
)

variables2003 <- c("q021501", "q021502", "q021503", "q021504", "q021505",
                   "q021506", "q021507", "q021508", "q021509", "q021510", 
                   "q021511", "q021512", "q021513")

for(i in variables2003) {
  n <- voter2003[,i] == 1.5 | voter2003[,i] == 2.5 | 
    voter2003[,i] == 3.5 | voter2003[,i] == 4.5 | voter2003[,i] == 11
  n[is.na(n)] <- FALSE
  voter2003[n, i] <- NA
}
voter2003 <- voter2003[rowSums(voter2003[,variables2003], na.rm = TRUE) != 0,]

irt2003_voter <- ltm::grm(voter2003[,variables2003], IRT.param = FALSE)

score2003 <- ltm::factor.scores(irt2003_voter, voter2003[,variables2003])


common2009 <- c("Q013802", "Q013804", "Q013805", "Q013809", 
                "Q013810", "Q013811", "Q013812", "Q013816")
common2003 <- c("q021501", "q021503", "q021504", "q021506", 
                "q021507", "q021508", "q021509", "q021513")
index <- data.frame(
  group1 = match(common2009, colnames(voter2009[,variables2009])), 
  group2 = match(common2003, colnames(voter2003[,variables2003]))
)

res2009 <- coef(irt2009_voter)
res2009 <- res2009[,c(5,1:4)]
colnames(res2009) <- c("a","b1","b2","b3","b4")

res2003 <- coef(irt2003_voter)
res2003 <- res2003[,c(5,1:4)]
colnames(res2003) <- c("a","b1","b2","b3","b4")

pm <- list(res2009, res2003)

rescat <- list(rep(5, nrow(res2009)), 
               rep(5, nrow(res2003)))

pm2009 <- as.poly.mod(n = nrow(res2009), model = "grm", 
                      items = 1:nrow(res2009))
pm2003 <- as.poly.mod(n = nrow(res2003), model = "grm", 
                      items = 1:nrow(res2003))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2009, pm2003), 
                   location = FALSE)

ideology <- list(score2009$score.dat$z1, score2003$score.dat$z1)

plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)

score2003_plink <- link.ability(plink_out)$group2
voter2003$Ideology <- score2003_plink

candidate2003 <- read_csv(
  "ates2v1.csv", 
  locale = locale(encoding = "CP932")
)
candidate2003 <- as.data.frame(candidate2003)

variables2003_2 <- c("defense", "treaty", "attack", "un", "iraq", 
                     "smallgov", "lifetime", "publicen", "keynes", "consump2", 
                     "postal", "highwayf", "foreign")

for(i in variables2003_2) {
  n <- candidate2003[,i] == "." | candidate2003[,i] == "9" | candidate2003[,i] == "1.5" | 
    candidate2003[,i] == "2.5" | candidate2003[,i] == "3.5" | candidate2003[,i] == "4.5"
  candidate2003[n, i] <- NA
  candidate2003[,i] <- as.numeric(candidate2003[,i])
}
candidate2003 <- candidate2003[rowSums(candidate2003[,variables2003_2], na.rm = TRUE) != 0,]

score2003_2 <- ltm::factor.scores(irt2003_voter, candidate2003[,variables2003_2])
ideology <- list(score2009$score.dat$z1, score2003_2$score.dat$z1)
plink_out2 <- plink(res, common = index, base.grp = 1, 
                    rescale = "MS", ability = ideology)
score2003_2_plink <- link.ability(plink_out2)$group2
candidate2003$Ideology <- score2003_2_plink

elected2003 <- filter(candidate2003, won != 4)
